/**
 * Created by apple on 2017/6/28.
 */

import React, {Component} from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  ListView,
  Image,
} from 'react-native';

import NavigationBar from './common/NaviagtionBar'

export default class FetchText extends Component {
  constructor(props) {
    super(props);
    this.state={
      result:'123'
    }
  }
  onLoad(url){
    this.setState({result:12345});
    fetch(url)
      .then((response) =>response.json())
      .then((result) =>{console.log(12345); this.setState({result: JSON.stringify(result)})})
      .catch(err =>{this.setState({result:JSON.stringify(err)})})
  }
  onSubmit(url,data){
    fetch(url,{
      method:'POST',
      header:{
        "Accept":'application/json',
        "Content-Type":'application/json'
      },
      body:JSON.stringify(data)
    })
      .then((response) =>response.json())
      .then((result) =>{console.log(12345); this.setState({result: JSON.stringify(result)})})
      .catch(err =>{this.setState({result:JSON.stringify(err)})})
  }
  render() {
    return (
      <View style={styles.container}>
        <NavigationBar
          title={'FetchText'}
          style={{backgroundColor: "#ee6363"}}
        />
        <Text onPress={() => this.onLoad('http://rapapi.org/mockjsdata/20823/adminManage/order',{uname:'123',pwd:'123456'})}>
          获取数据
        </Text>
        <Text>
          结果是:
        </Text>
        <Text onPress={() => this.onSubmit('http://rapapi.org/mockjsdata/20823/adminManage/order')}>
          提交数据
        </Text>
        <Text>
          结果是:
          {this.state.result}
        </Text>
      </View>
    )
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#f5f5ff'
  },
  text: {
    fontSize: 18
  },
  row: {
    height: 50
  },
  line: {
    height: 1,
    backgroundColor: "#000"
  }
});





